<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Column;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;

class ColumnController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('栏目管理');
            $content->description('列表');
//            $content->body($this->grid());
            $content->body(Column::tree(function ($tree) {
                $tree->disableSave();
                $tree->query(function ($model) {
                    return $model->where('type', 0);
                });

            }));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('栏目管理');
            $content->description('编辑');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('栏目管理');
            $content->description('创建');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Column::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->title('标题');
            $grid->column('type','类别')->display(function ($type){
                switch ($type)
                {
                    case 0:
                        return '网站';
                        break;
                    case 1:
                        return '文章';
                        break;
                }
            });
            $grid->disableExport(); //禁用导出数据按钮
            $grid->disableRowSelector(); //禁用行选择checkbox
            $grid->actions(function ($actions) {
                $actions->disableDelete();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Column::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->select('type', '类别')->options([0 => '网站', 1 => '文章'])->rules('required',[
                'required' => '请选择类别',
            ]);
            $form->select('parent_id', '父级菜单')->options(Column::selectOptions());
            $form->text('title', '标题')->rules('required');
            $form->number('order', '排序')->min(0)->max(10)->rules('required');
            $form->checkbox('state', '状态')->options([0 => '不显示', 1 => '推荐', 2 => '置顶', 3 => '前台显示']);
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
